using System;
using System.Collections;
using BCW;
using I2.Loc;
using UnityEngine;

public class GotchaChestUI : MonoBehaviour
{
	public GotchaChestType type;

	[HideInInspector]
	public GotchaChest gotchaChest;

	public new UILabel name;

	public UIBasicSprite texture;

	public UISprite selectedBG;

	public GameObject disabledFG;

	public UILabel timerLabel;

	public GameObject timerObject;

	public UILabel countLabel;

	public GameObject countObject;

	public GameObject openButton;

	public UILabel openLabel;

	public MarketPriceTag openPriceTag;

	public GameObject arrow;

	private bool selected;

	private bool initialized;

	private bool canOpenFree;

	private Coroutine timerCoroutine;

	private void OnClick()
	{
		Select(true);
	}

	public void Init(GotchaChestType gotchaChestType)
	{
		if (!initialized)
		{
			type = gotchaChestType;
			bool flag = true;
			switch (type)
			{
			case GotchaChestType.Wooden:
				gotchaChest = GotchaManager.instance.WoodenChest;
				break;
			case GotchaChestType.Bronze:
				gotchaChest = GotchaManager.instance.BronzeChest;
				break;
			case GotchaChestType.Silvern:
				gotchaChest = GotchaManager.instance.SilvernChest;
				break;
			case GotchaChestType.Golden:
				gotchaChest = GotchaManager.instance.GoldenChest;
				break;
			case GotchaChestType.Non:
				Debug.LogError("GotchaChestType is Non!");
				gotchaChest.type = GotchaChestType.Non;
				flag = false;
				break;
			}
			name.text = ScriptLocalization.Get("Game_Common/" + gotchaChest.type);
			base.gameObject.name = "Chest_" + gotchaChest.type;
			gotchaChest.countChanged += UpdateCestUI;
			gotchaChest.unlockTimeChanged += UpdateTimerAndUI;
			JSONManager.Instance.serverTimeUpdate += UpdateTimerAndUI;
			if (gotchaChest.type.Equals(GotchaChestType.Wooden))
			{
				GetComponentInChildren<InGameTutorialBtn>().enableOnStart.Add(arrow);
			}
			gotchaChest.ResetTimer();
			Select(false);
			initialized = flag;
		}
	}

	private void OnEnable()
	{
		if (gotchaChest != null && gotchaChest.type != 0)
		{
			UpdateTimerAndUI();
		}
	}

	private void OnDestroy()
	{
		gotchaChest.countChanged -= UpdateCestUI;
		gotchaChest.unlockTimeChanged -= UpdateTimerAndUI;
		JSONManager.Instance.serverTimeUpdate -= UpdateTimerAndUI;
		if (timerCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(timerCoroutine);
			timerCoroutine = null;
		}
	}

	private void UpdateCestUI()
	{
		texture = texture.ReplaceForCurrentPlatform();
		if (gotchaChest.IsUnlocked())
		{
			timerObject.SetActive(false);
			openPriceTag.gameObject.SetActive(false);
			openLabel.text = ScriptLocalization.Get("Game_Common/chest_open");
		}
		else
		{
			timerObject.SetActive(true);
			openPriceTag.gameObject.SetActive(true);
			openLabel.text = ScriptLocalization.Get("Game_Common/chest_unlock");
		}
		if (gotchaChest.count > 0)
		{
			openButton.SetActive(true);
			disabledFG.SetActive(false);
			if (gotchaChest.count > 1)
			{
				countLabel.text = "x" + gotchaChest.count;
				countObject.SetActive(true);
			}
			else
			{
				countObject.SetActive(false);
			}
			if (gotchaChest.IsUnlocked())
			{
				texture.LoadTextureAndAlphaMask(settings.shopTextureFolderPath, string.Concat("Open_Сhest_", gotchaChest.type, "_Old"));
			}
			else
			{
				texture.LoadTextureAndAlphaMask(settings.shopTextureFolderPath, string.Concat("Closed_Сhest_", gotchaChest.type, "_Old"));
			}
		}
		else
		{
			openButton.SetActive(false);
			disabledFG.SetActive(true);
			countObject.SetActive(false);
			timerObject.SetActive(false);
			texture.LoadTextureAndAlphaMask(settings.shopTextureFolderPath, string.Concat("Disabled_Сhest_", gotchaChest.type, "_Old"));
		}
	}

	public void UpdateTimerAndUI()
	{
		gotchaChest.ResetTimer();
		if (timerCoroutine != null)
		{
			CoroutineRunner.Instance.StopCoroutine(timerCoroutine);
			timerCoroutine = null;
		}
		UpdateCestUI();
		if (!gotchaChest.IsUnlocked())
		{
			timerCoroutine = CoroutineRunner.Instance.StartCoroutine(TimerIEnumerator(gotchaChest.unlockTime - JSONManager.Instance.GetServerTime()));
		}
	}

	private IEnumerator TimerIEnumerator(int secondsToOpen)
	{
		for (int currentTimer = secondsToOpen; currentTimer > 0; currentTimer--)
		{
			gotchaChest.SetActualOpenPrice();
			openPriceTag.SetPrice(gotchaChest.openMoney, gotchaChest.actualOpenPrice);
			TimeSpan t = TimeSpan.FromSeconds(currentTimer);
			string time = ((t.Hours > 0) ? ((t.Hours <= 9) ? string.Format("{0:D1}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}:{2:D2}", t.Hours, t.Minutes, t.Seconds)) : ((t.Minutes > 0) ? ((t.Minutes <= 9) ? string.Format("{0:D1}:{1:D2}", t.Minutes, t.Seconds) : string.Format("{0:D2}:{1:D2}", t.Minutes, t.Seconds)) : ((t.Seconds <= 9) ? string.Format("{0:D1}", t.Seconds) : string.Format("{0:D2}", t.Seconds))));
			timerLabel.text = time;
			yield return new WaitForSeconds(1f);
		}
		UpdateCestUI();
	}

	public void Select(bool selected)
	{
		if (this.selected == selected)
		{
			return;
		}
		if (selected)
		{
			if (!initialized || gotchaChest.count == 0)
			{
				return;
			}
			GotchaManagerUI.instance.SelectChest(this);
			base.transform.localScale = Vector3.one * 1.05f;
			selectedBG.gameObject.SetActive(true);
		}
		else
		{
			base.transform.localScale = Vector3.one;
			selectedBG.gameObject.SetActive(false);
		}
		this.selected = selected;
	}

	public bool UnlockChest()
	{
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			controllerMenu.thisScript.ShowNoInternet();
			return false;
		}
		bool result = gotchaChest.Unlock();
		UpdateTimerAndUI();
		return result;
	}

	public void OpenChest()
	{
		StartCoroutine(OpenChestWithAnimation());
	}

	private IEnumerator OpenChestWithAnimation()
	{
		if (!JSONManager.Instance.ServerTimeLoad())
		{
			controllerMenu.thisScript.ShowNoInternet();
		}
		else if (gotchaChest.count > 0)
		{
			Select(true);
			if (UnlockChest())
			{
				texture = texture.ReplaceForCurrentPlatform();
				gotchaChest.Open();
				texture.LoadTextureAndAlphaMask(settings.shopTextureFolderPath, string.Concat("Open_Сhest_", gotchaChest.type, "_Old"));
				openButton.gameObject.SetActive(false);
				timerObject.SetActive(false);
				GotchaManagerUI.instance.Spin();
			}
		}
		yield break;
	}
}
